package com.probiz.estore.product.dao.impl;

import org.hibernate.Query;

import com.probiz.estore.common.model.product.Brand;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.product.dao.BrandDao;

/**
 * Dao implementation for Brand.
*/
public class BrandDaoImpl extends HibernateGenericDaoImpl<Brand> implements BrandDao {
	
	public boolean isBrandExist(String brandName) {
        Long brandCount = this.countByHql("from Brand where brandName=?", brandName);
        if (brandCount != null && brandCount==0) {
            logger.debug("the brandName has not in database.");
            return false;
        } else {
            return true;
        }
    }
	
	public boolean isBrandNameUseByOthers(String brandName, Integer brandId) {
        String hql = "select count(*) from Brand where brandName =:brandName and brandId<>:brandId";
        Query query = getSession().createQuery(hql);

        query.setString("brandName", brandName);
        query.setInteger("brandId", brandId.intValue());
        int countNumber = ((Long) query.uniqueResult()).intValue();

        if (countNumber > 0) {
            return true;
        } else {
            return false;
        }
    }
}
